﻿// -----------------------------------------------------------------------
// <copyright file="Histogram.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Histogram
    {
        /// <summary>
        /// static object of this class to use this without create object
        /// </summary>
        private static Histogram histogram = new Histogram();

        /// <summary>
        /// buffer of input image
        /// </summary>
        private Color[,] bufferOfImage;

        /// <summary>
        /// Gets or sets the histogram1.
        /// </summary>
        /// <value>
        /// The histogram1.
        /// </value>
        public static Histogram Histogram1
        {
            get { return Histogram.histogram; }
            set { Histogram.histogram = value; }
        }

        /// <summary>
        /// Gets the max value in long array.
        /// </summary>
        /// <param name="data">The array.</param>
        /// <returns>
        /// max value repetion
        /// </returns>
        public long GetMax(long[] data)
        {
            long max = 0;
            foreach (long temp in data)
            {
                if (temp > max)
                {
                    max = temp;
                }
            }

            return max;
        }

        /// <summary>
        /// Draws the histogram red of image.
        /// </summary>
        /// <param name="image">The image need histogram of it.</param>
        /// <returns>
        /// array of the #of pxls
        /// </returns>
        public long[] HistoRed(Bitmap image)
        {
            this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
            long[] histogram = new long[256];
            histogram[0] = 0;
            try
            {
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        histogram[this.bufferOfImage[j, i].R]++;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }

            return histogram;
        }

        /// <summary>
        /// Histoes the green.
        /// </summary>
        /// <param name="image">The image need histogram of it.</param>
        /// <returns>
        /// array of the #of pxls
        /// </returns>
        /// Draws the histogram red of image.
        public long[] HistoGreen(Bitmap image)
        {
            this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
            long[] histogram = new long[256];
            histogram[0] = 0;
            try
            {
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        histogram[this.bufferOfImage[j, i].G]++;
                    }
                }
            }
            catch (Exception eX)
            {
                Logger.LogException(eX);
                throw eX;
            }

            return histogram;
        }

        /// <summary>
        /// Histoes the blue.
        /// </summary>
        /// <param name="image">The image need histogram of it.</param>
        /// <returns>
        /// array of the #of pxls
        /// </returns>
        /// Draws the histogram red of image.
        public long[] HistoBlue(Bitmap image)
        {
            this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
            long[] histogram = new long[256];
            histogram[0] = 0;
            try
            {
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        histogram[this.bufferOfImage[j, i].B]++;
                    }
                }
            }
            catch (Exception eX)
            {
                Logger.LogException(eX);
                throw eX;
            }

            return histogram;
        }

        /// <summary>
        /// Histoes the gray.
        /// </summary>
        /// <param name="image">The image need histogram of it.</param>
        /// <returns>
        /// array of the #of pxls
        /// </returns>
        /// Draws the histogram red of image.
        public long[] HistoGray(Bitmap image)
        {
            this.bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
            long[] histogram = new long[256];
            histogram[0] = 0;
            try
            {
                for (int i = 0; i < this.bufferOfImage.GetLength(1); i++)
                {
                    for (int j = 0; j < this.bufferOfImage.GetLength(0); j++)
                    {
                        histogram[(this.bufferOfImage[j, i].R + this.bufferOfImage[j, i].G + this.bufferOfImage[j, i].B) / 3]++;
                    }
                }
            }
            catch (Exception eX)
            {
                Logger.LogException(eX);
                throw eX;
            }

            return histogram;
        }

        /// <summary>
        /// Draws the specified table.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <param name="c">The gragh color</param>
        /// <param name="heigthOfImage">The heigth of image.</param>
        /// <returns>
        /// the gragh image
        /// </returns>
        public Bitmap Draw(long[] table, Color c, int heigthOfImage)
        {
            Bitmap graph = new Bitmap(256, heigthOfImage);
            double[] graph1 = new double[256];
            int sizeOfdraw = heigthOfImage - 5;
            try
            {
                long max = this.GetMax(table);
                for (int i = 0; i < 256; i++)
                {
                    try
                    {
                        graph1[i] = (table[i] * sizeOfdraw) / max;
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex);
                        throw ex;
                    }
                }

                for (int i = 0; i < graph.Width; i++)
                {
                    for (int j = 0; j < graph.Height; j++)
                    {
                        if (j >= graph.Height - graph1[i])
                        {
                            graph.SetPixel(i, j, c);
                        }
                        else
                        {
                            graph.SetPixel(i, j, Color.WhiteSmoke);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }

            return graph;
        }
    }
}
